package com.shareach.search.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.type.TypeReference;

import com.shareach.basic.HttpBase;
import com.shareach.basic.ServiceException;
import com.shareach.search.weibo.domain.POI;
import com.shareach.search.weibo.domain.POIList;
import com.shareach.search.weibo.domain.TipList;
import com.shareach.search.weibo.domain.TipPhotoList;
import com.shareach.search.weibo.domain.UserList;
import com.shareach.search.weibo.domain.WeiboPOI;
import com.shareach.util.JsonUtil;
import com.shareach.util.URLEncodeUtils;
import com.shareach.web.basic.Answer;

public class WeiboSearcher extends HttpBase {
	private String host;
	public WeiboSearcher(String host){
		this.host = host;
	}

	/**
	 * 匹配商户信息
	 * @param snatchHost
	 * @param city
	 * @param brandName
	 * @param name
	 * @param addr
	 * @param tels
	 * @return
	 */
	public Answer<WeiboPOI> matchShop(String city, String brandName, String name, String addr, String... tels){
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("city", city);
		params.put("name", name);
		params.put("brandName", brandName);
		params.put("addr", addr);
		if(tels!=null && tels.length>0){
			String sTel = "";
			for(String s:tels){
				if(sTel.length()>0){
					sTel=","+s;
				}else{
					sTel = s;
				}
			}
			params.put("tel", sTel);
		}
		String json = this.httpPost(host + "/weibo/match.do", params, null);
		return JsonUtil.objectFromJson(json, new TypeReference<Answer<WeiboPOI>>(){});// new TypeRefer)
	}
	/**
	 * @param city 城市代码，默认为全国搜索。
	 * @param category	查询的分类代码，取值范围见：分类代码对应表。
	 * @param keyword 查询的关键词，必须进行URLencode。
	 * @param page 返回结果的页码，默认为1。
	 * @param count 单页返回的记录条数，默认为20，最大为50。
	 */
	public POIList search(String city, String category, String keyword, Integer page, Integer count){
		StringBuffer sb = new StringBuffer();
		sb.append("search.do?city="+city);
		if(!StringUtils.isBlank(category))sb.append("&category="+category);
		if(!StringUtils.isBlank(keyword))sb.append("&keyword="+URLEncodeUtils.encodeURL(keyword));
		if(page!=null)sb.append("&page="+page);
		if(count!=null)sb.append("&count="+count);
		String json =  this.httpGet(host+"/weibo/poi/"+sb.toString(),null);
		POIList result = null;
		if(json.equals("[]")){
			result = new POIList();
			result.page = page;
			result.total_number = 0;
			return result;
		}
		try{
			Answer<POIList> jsonResult =JsonUtil.objectFromJson(json, new TypeReference<Answer<POIList>>(){});
			if(jsonResult.getCode()==0){
				result = jsonResult.getResult();
			}else{
				result = new POIList();
				result.page = page;
				result.total_number = 0;
				return result;
			}
		}catch(Exception exp){
			throw new ServiceException(-1,exp.getMessage());
		}
		if(StringUtils.isBlank(result.error_code)){
			return result;
		}else{
			throw new ServiceException(Integer.parseInt(result.error_code),"搜索POI错误");
		}
	}

	/**
	 * 获取POI 信息
	 * @param poiid
	 * @return
	 */
	public POI getPOI(String poiid){
		String json =  this.httpGet(host+"/weibo/poi/get.do?poiid="+poiid,null);
		POI poi = null;
		try{
			//poi = JsonUtil.objectFromJson(json, POI.class);

			Answer<POI> jsonResult =JsonUtil.objectFromJson(json, new TypeReference<Answer<POI>>(){});
			if(jsonResult.getCode()==0){
				poi = jsonResult.getResult();
			}
		}catch(Exception exp){
			throw new ServiceException(-1,exp.getMessage());
		}
		if(poi!=null && StringUtils.isBlank(poi.error_code)){
			return poi;
		}else{
			throw new ServiceException(Integer.parseInt(poi.error_code),"获取POI错误");
		}
	}
	
	public List<POI> getPOIs(String...poiids){
		List<POI> pois = new ArrayList<POI>();
		for(String poiid:poiids){
			try{pois.add(getPOI(poiid));}catch(Exception e){}
		}
		return pois;
	}
	
	/**
	 * 获取指定poi的点评信息
	 * @param poiid
	 * @param page
	 * @param count
	 * @return
	 */
	public TipList getPOITips(String poiid, Integer page, Integer count){
		StringBuffer sb = new StringBuffer();
		sb.append("tips.do?poiid="+poiid);
		if(page!=null)sb.append("&page="+page);
		if(count!=null)sb.append("&count="+count);
		String json = httpGet(host+"/weibo/poi/"+sb.toString(),null);
		TipList  result = null;

		try{
			if(json.equals("[]")){
				result = new TipList();
				result.total_number = 0;
			}else{
				Answer<TipList> jsonResult =JsonUtil.objectFromJson(json, new TypeReference<Answer<TipList>>(){});
				if(jsonResult.getCode()==0){
					result = jsonResult.getResult();
				}
			}
			result.page = page;
		}catch(Exception exp){
			throw new ServiceException(-1,exp.getMessage());
		}
		if(StringUtils.isBlank(result.error_code)){
			return result;
		}else{
			throw new ServiceException(Integer.parseInt(result.error_code),"获取poi的点评信息失败");
		}
	}
	
	/**
	 * 获取指定poi的图片列表
	 * @param poiid
	 * @param page
	 * @param count
	 * @return
	 */
	public TipPhotoList getPOIPhotoes(String poiid, Integer page, Integer count){
		StringBuffer sb = new StringBuffer();
		sb.append("photoes.do?poiid="+poiid);
		if(page!=null)sb.append("&page="+page);
		if(count!=null)sb.append("&count="+count);
		String json = httpGet(host+"/weibo/poi/"+sb.toString(),null);
		TipPhotoList  result = null;
		try{
			if(json.equals("[]")){
				result = new TipPhotoList();
				result.total_number = 0;
			}else{
				//result = JsonUtil.objectFromJson(json, TipPhotoList.class);
				Answer<TipPhotoList> jsonResult =JsonUtil.objectFromJson(json, new TypeReference<Answer<TipPhotoList>>(){});
				if(jsonResult.getCode()==0){
					result = jsonResult.getResult();
				}
			}
			result.page = page;
		}catch(Exception exp){
			throw new ServiceException(-1,exp.getMessage());
		}
		if(StringUtils.isBlank(result.error_code)){
			return result;
		}else{
			throw new ServiceException(Integer.parseInt(result.error_code),"获取POI的照片数据失败");
		}
	}
	
	/**
	 * 获取指定poi签到用户
	 * @param poiid
	 * @param page
	 * @param count
	 * @return
	 */
	public UserList getPOIUsers(String poiid, Integer page, Integer count){
		StringBuffer sb = new StringBuffer();
		sb.append("users.do?poiid="+poiid);
		if(page!=null)sb.append("&page="+page);
		if(count!=null)sb.append("&count="+count);
		String json = httpGet(host+"/weibo/poi/"+sb.toString(),null);
		UserList result = null;

		try{
			//result = JsonUtil.objectFromJson(json, UserList.class);
			Answer<UserList> jsonResult =JsonUtil.objectFromJson(json, new TypeReference<Answer<UserList>>(){});
			if(jsonResult.getCode()==0){
				result = jsonResult.getResult();
			}
		}catch(Exception exp){
			throw new ServiceException(-1,exp.getMessage());
		}
		if(StringUtils.isBlank(result.error_code)){
			return result;
		}else{
			throw new ServiceException(Integer.parseInt(result.error_code),"获取签到用户列表失败");
		}
	}
}
